/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.netcracker.test.ten;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author I
 */
public class Item3 {
    File fl = null;
    public void printDirectory(String path){
        StringBuilder count_space = new StringBuilder();
        count_space.append(' ');
        fl = new File(path);
        ArrayList<Integer> list_i = new ArrayList<Integer>();
        File[]arf = fl.listFiles();
        int i = 0;
        
        list_i.add(i);
        System.out.println(fl.getName());
        
        if(arf == null){
            System.out.println("File not found!");
            return;
        }
            
        while(true){
            if(i < arf.length){
                if(arf[i].listFiles() == null){
                    System.out.println(count_space + arf[i].getName());
                    i++;
                    list_i.remove(list_i.size() - 1);
                    list_i.add(i);
                }
                else{
                    if(arf[i].listFiles().length != 0){
                        System.out.println(count_space + arf[i].getName());
                        count_space.append(' ');
                        arf = arf[i].listFiles();
                        i = 0;
                        list_i.add(i);
                    }
                    else{
                        System.out.println(count_space + arf[i].getName());
                        i++;
                        list_i.remove(list_i.size() - 1);
                        list_i.add(i);
                    }
                }
            }
            else{
                if(list_i.size() > 1){
                    arf = arf[i - 1].getParentFile().getParentFile().listFiles();
                    list_i.remove(list_i.size() - 1);
                    count_space.deleteCharAt(count_space.length() - 1);
                    i = list_i.get(list_i.size() - 1) + 1;
                    list_i.remove(list_i.size() - 1);
                    list_i.add(i);
                }
                else{
                    break;
                }
            }
         
        }
    }
}
